package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.domain.CClearPlan;
import cc.rengu.redp.bizimpl.csmc.dao.CClearPlanMapper;
import cc.rengu.redp.bizimpl.csmc.domain.CClearPlanMap;
import cc.rengu.redp.bizimpl.csmc.service.ICClearPlanMapService;
import cc.rengu.redp.bizimpl.csmc.service.ICClearPlanService;
import cc.rengu.redp.bizimpl.csmc.vo.ClearPlanVO;
import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizDataStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.domain.BizTmp;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpPrivilegeCheck;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.redis.RedisService;
import cc.rengu.redp.common.utils.SortUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author lizk
 */
@Service
@DS("csmc")
public class CClearPlanServiceImpl extends ServiceImpl<CClearPlanMapper, CClearPlan> implements ICClearPlanService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IBizHisService bizHisService;
    @Autowired
    private IUpmpSysSeqService sysSeqService;
    @Autowired
    private ICClearPlanMapService clearPlanMapService;
    @Autowired
    private RedisService redisService;

    private long oprMenuId = 1;
    private String oprMenuName = "清分清算管理:清分清算方案资金管理";
    private long chkMenuId = 2;
    private String chkMenuName = "清分清算管理:清分清算方案资金管理";
    private String webNextUrl = "/admin/csmc/clearPlan";


    @Override
    public IPage<CClearPlan> findByPage(QueryRequest queryRequest, CClearPlan clearPlan) {

        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        try {
            String instId = clearPlan.getInstId();
            if (Common.isBlank(instId)) {
                instId = user.getInstId();
            }
            LambdaQueryWrapper<CClearPlan> queryWrapper = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmpty(instId)) {
                queryWrapper.eq(CClearPlan::getInstId, instId);
            }
            if (!StringUtils.isEmpty(clearPlan.getClearPlanId())) {
                queryWrapper.eq(CClearPlan::getClearPlanId, clearPlan.getClearPlanId());
            }
            Page<CClearPlan> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page, true);
            return this.page(page, queryWrapper);
        } catch (Exception e) {
            log.error("获取清分清算方案资金配置信息失败", e);
            return null;
        }
    }

    @Override
    public void oprClearPlan(ClearPlanVO clearPlanVO) throws Exception {
        if (Common.isBlank(clearPlanVO.getClearPlanList().get(0).getClearPlanId())) {
            //redis获取清分清算方案标识，只在此处用到
            String clearPlanId = redisService.get("clearPlanId");
            if (clearPlanId.isEmpty()) {
                log.error("请求参数清分清算方案标识(ClearPlanId)异常!");
                throw new RedpException("请求参数清分清算方案标识(ClearPlanId)异常!");
            }
            clearPlanVO.getClearPlanList().get(0).setClearPlanId(clearPlanId);
            clearPlanVO.setCheckStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_NOTCOMMIT.getType());
        }
        saveData(clearPlanVO.getClearPlanList(), clearPlanVO.getOptFlag(), clearPlanVO.getCheckStatus(),clearPlanVO.getCid());
    }

    private void saveData(List<CClearPlan> clearPlanList, String flag, String checkStatus,String cid) throws RedpException {

        String clearPlanMapId = clearPlanList.get(0).getClearPlanId();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        String clearplanid = clearPlanList.get(0).getClearPlanId();
        String oprstatus = clearPlanList.get(0).getOprStatus();
        String auditStatus = clearPlanList.get(0).getAuditStatus();
        String instId = clearPlanList.get(0).getInstId();
        String id = clearPlanList.get(0).getId();

        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId, "clearPlan:add");

            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                // 保存正式表
                for (int idx = 0; idx < clearPlanList.size(); idx++) {
                    CClearPlan clearplan = clearPlanList.get(idx);
                    clearplan.setId(null);
                    clearplan.setInstId(instId);
                    clearplan.setOprId(user.getId());
                    clearplan.setCreateTime(lastUpddt);
                    clearplan.setAuditOprId(user.getId());
                    clearplan.setUpdateTime(lastUpddt);
                    clearplan.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                }
                this.mergeData(clearPlanList);

                // 保存临时表
                List<BizTmp> bizTmpList = new ArrayList<>();
                for (CClearPlan clearplan : clearPlanList) {
                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(clearplan);
                    if (bizData.length() > 12000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    String ename = clearplan.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = clearplan.getClass().getAnnotation(Description.class).value();

                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData1 = bizData.substring(0, 3999);
                        bizData2 = bizData.substring(4000);
                    } else {
                        bizData1 = bizData;
                    }
                    if (bizData2.length() > 4000) {
                        bizData2 = bizData2.substring(0, 3999);
                        bizData3 = bizData.substring(8000);
                    }
                    tmp.setBizData1(bizData1);
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setRealId(clearplan.getId());
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(chkMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                    tmp.setInstId(instId);
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    tmp.setWebNextUrl(webNextUrl);
                    tmp.setDetailFlag("Y");
                    tmp.setMasterKey(clearPlanMapId);
                    tmp.setBizOpr(BizOprEnum.OPR_ADD.getType());
                    bizTmpList.add(tmp);


                }
                bizTmpService.saveOrUpdateBatch(bizTmpList);

                //更新第一个tab页新增-审批流程中
                clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());

            } else {
                // 保存正式表
                for (int idx = 0; idx < clearPlanList.size(); idx++) {
                    CClearPlan clearplan = clearPlanList.get(idx);
                    clearplan.setId(id);
                    clearplan.setInstId(instId);
                    clearplan.setOprId(user.getId());
                    clearplan.setCreateTime(lastUpddt);
                    clearplan.setAuditOprId(user.getId());
                    clearplan.setUpdateTime(lastUpddt);
                    clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                }
                this.mergeData(clearPlanList);
                for (CClearPlan clearplan : clearPlanList) {
                    // 保存历史表
                    bizHisService.saveBizHisNew(clearplan, clearplan, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt,
                            String.valueOf(oprMenuId), oprMenuName, clearplan.getId(), null, null, null);

                }
                //将第一个tab页面数据改为正式 新增审批通过
                CClearPlanMap clearPlanMap = clearPlanMapService.findByClearPlanId(cid);
                clearPlanMap.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                clearPlanMap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                clearPlanMapService.saveOrUpdate(clearPlanMap);

            }
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(instId, "clearPlan:edit");
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                String tmpBizOpr = BizOprEnum.OPR_UPDATE.getType();
                String tmpAuditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType();
                if (!BizDataAuditStatusEnum.AUDIT_STATUS_ADD_NOTCOMMIT.getType().equals(clearPlanList.get(0).getAuditStatus())) {
                    // 修改第二个tab页正式表的审核状态
                    this.upstatus(clearplanid, BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType(), clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);

                    //更新第一个tab页修改-审批流程中
                    clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                } else {
                    tmpBizOpr = BizOprEnum.OPR_ADD.getType();
                    tmpAuditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType();
                    //新增待审核的数据--修改后入正式表
                    for (int idx = 0; idx < clearPlanList.size(); idx++) {
                        CClearPlan clearplan = clearPlanList.get(idx);
                        clearplan.setId(null);
                        clearplan.setInstId(instId);
                        clearplan.setOprId(user.getId());
                        clearplan.setCreateTime(lastUpddt);
                        clearplan.setAuditOprId(user.getId());
                        clearplan.setUpdateTime(lastUpddt);
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                    }
                    this.mergeData(clearPlanList);
                    //更新第一个tab页新增-审批流程中
                    clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                }
                //修改临时表的数据
                List<BizTmp> bizTmpList = new ArrayList<>();
                for (CClearPlan clearplan : clearPlanList) {
                    clearplan.setAuditStatus(tmpAuditStatus);
                    BizTmp tmp = new BizTmp();
                    String bizData = JSON.toJSONString(clearplan);
                    if (bizData.length() > 12000) {
                        throw new RedpException("业务数据过长，请联系管理员！");
                    }
                    String ename = clearplan.getClass().getAnnotation(TableName.class).value().toLowerCase();
                    String cname = clearplan.getClass().getAnnotation(Description.class).value();

                    String bizData1;
                    String bizData2 = "";
                    String bizData3 = "";
                    if (bizData.length() > 4000) {
                        bizData1 = bizData.substring(0, 3999);
                        bizData2 = bizData.substring(4000);
                    } else {
                        bizData1 = bizData;
                    }
                    if (bizData2.length() > 4000) {
                        bizData2 = bizData2.substring(0, 3999);
                        bizData3 = bizData.substring(8000);
                    }

                    tmp.setOprId(user.getId());
                    tmp.setBizData1(bizData1);
                    tmp.setBizData2(bizData2);
                    tmp.setBizData3(bizData3);
                    tmp.setBizOpr(tmpBizOpr);
                    tmp.setCreateTime(lastUpddt);
                    tmp.setLastOprId(user.getId());
                    tmp.setLastUpdateTime(lastUpddt);
                    tmp.setOprMenuId(String.valueOf(oprMenuId));
                    tmp.setOprMenuName(chkMenuName);
                    tmp.setOprTableCname(cname);
                    tmp.setOprTableEname(ename);
                    tmp.setManageInstId(user.getInstId());
                    tmp.setInstId(instId);
                    tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    tmp.setAuditStatus(tmpAuditStatus);
                    tmp.setDetailFlag("Y");
                    tmp.setMasterKey(clearPlanMapId);

                    tmp.setRealId(clearplan.getId());

                    tmp.setWebNextUrl(webNextUrl);
                    bizTmpList.add(tmp);
                }
                this.tmpData(bizTmpList, clearPlanMapId, instId);
            } else {
                //正式表修改之前的数据
                List<CClearPlan> dbclearplan = getListByClearPlanId(clearplanid);
                // 修改正式表
                for (int idx = 0; idx < clearPlanList.size(); idx++) {
                    CClearPlan clearplan = clearPlanList.get(idx);
                    clearplan.setAuditOprId(user.getId());
                    clearplan.setUpdateTime(lastUpddt);
                    clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                    clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                }
                this.mergeData(clearPlanList);

                //正式表修改后的数据
                List<CClearPlan> clearplanNew = getListByClearPlanId(clearplanid);
                this.hisData(dbclearplan, clearplanNew, String.valueOf(oprMenuId), chkMenuName, user, user.getInstId(), lastUpddt);

                //将第一个tab页面数据改为正式 新增审批通过
                CClearPlanMap clearPlanMap = clearPlanMapService.findByClearPlanId(cid);
                clearPlanMap.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                clearPlanMap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                clearPlanMapService.saveOrUpdate(clearPlanMap);
            }
        } else if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)
                || PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {

            List entityOlds = getListByClearPlanId(clearplanid);//修改之前
            List<String> bizOpr = new ArrayList<>();//状态
            List<String> realIds = new ArrayList<>();//id

            UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
            if (PageOptFlagEnum.OPT_FLAG_STOP.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清分清算方案管理:停用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType();
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    //比较更新临时表数据
                    this.updateTmp(clearPlanList, BizOprEnum.OPR_STOP.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType(),
                            String.valueOf(oprMenuId), chkMenuName, webNextUrl, clearPlanMapId, instId, user, user.getInstId());

                    //更新第一个tab页停用-停用流程中
                    clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, auditStatus);
                } else {
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        realIds.add(clearplan.getId());
                    }
                    oprstatus = BizDataStatusEnum.STATUS_STOPED.getType();
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType();
                    List entitys = clearPlanList;//修改之后

                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);

                    //将第一个tab页正式表状态和审核状态
                    CClearPlanMap clearPlanMap = clearPlanMapService.findByClearPlanId(cid);
                    clearPlanMap.setOprStatus(oprstatus);
                    clearPlanMap.setAuditStatus(auditStatus);
                    clearPlanMapService.saveOrUpdate(clearPlanMap);
                }
            } else if (PageOptFlagEnum.OPT_FLAG_USE.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清分清算方案管理:启用");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType();
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    //比较更新临时表数据
                    this.updateTmp(clearPlanList, BizOprEnum.OPR_USE.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType(),
                            String.valueOf(oprMenuId), chkMenuName, webNextUrl, clearPlanMapId, instId, user, user.getInstId());

                    //更新第一个tab页停用-启用流程中
                    clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, auditStatus);
                } else {
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        realIds.add(clearplan.getId());
                    }
                    oprstatus = BizDataStatusEnum.STATUS_NORMAL.getType();
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType();
                    List entitys = clearPlanList;//修改之后
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);

                    //将第一个tab页正式表状态和审核状态
                    CClearPlanMap clearPlanMap = clearPlanMapService.findByClearPlanId(cid);
                    clearPlanMap.setOprStatus(oprstatus);
                    clearPlanMap.setAuditStatus(auditStatus);
                    clearPlanMapService.update(clearPlanMap);
                }
            } else if (PageOptFlagEnum.OPT_FLAG_CANCEL.getType().equals(flag)) {
                privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "清分清算方案管理:注销");
                if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType();
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    //比较更新临时表数据
                    this.updateTmp(clearPlanList, BizOprEnum.OPR_CANCEL.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType(),
                            String.valueOf(oprMenuId), chkMenuName, webNextUrl, clearPlanMapId, instId, user, user.getInstId());

                    //更新第一个tab页停用-注销流程中
                    clearPlanMapService.updateCleaPlanMapAuditStatus(clearPlanMapId, auditStatus);
                } else {
                    for (CClearPlan clearplan : clearPlanList) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        realIds.add(clearplan.getId());
                    }
                    oprstatus = BizDataStatusEnum.STATUS_CANCELED.getType();
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType();
                    List entitys = clearPlanList;//修改之后
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);

                    //将第一个tab页正式表状态和审核状态
                    CClearPlanMap clearPlanMap = clearPlanMapService.findByClearPlanId(cid);
                    clearPlanMap.setOprStatus(oprstatus);
                    clearPlanMap.setAuditStatus(auditStatus);
               /*     LambdaUpdateWrapper<CClearPlanMap> wrapper = new LambdaUpdateWrapper<>();
                    wrapper.eq(CClearPlanMap::getInstId, clearPlanMap.getInstId());
                    wrapper.eq(CClearPlanMap::getMatchKey, clearPlanMap.getMatchKey());*/
                    clearPlanMapService.update(clearPlanMap);
                }
            }
            //无论是否需要审批 更新第二个tab页正式表状态和审核状态
            this.upstatus(clearplanid, auditStatus, oprstatus, user.getId(), lastUpddt);
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {
            //临时表数据
            List<BizTmp> bizTmpByMasterKey = bizTmpService.getBizTmpByMasterKey(instId, clearPlanMapId);
            List<CClearPlan> clearPlanListNew = new ArrayList<>();
            for (BizTmp biztmp : bizTmpByMasterKey) {
                String bizdata1 = "";
                if (null != biztmp.getBizData1()) {
                    bizdata1 += biztmp.getBizData1();
                }
                if (null != biztmp.getBizData2()) {
                    bizdata1 += biztmp.getBizData2();
                }
                if (null != biztmp.getBizData3()) {
                    bizdata1 += biztmp.getBizData3();
                }
                CClearPlan clearplan = JSON.parseObject(bizdata1, CClearPlan.class);
                //修改操作中新增的数据存在于临时表，所以需要赋值ID
                if (Common.isBlank(clearplan.getId())) {
                    clearplan.setId(biztmp.getRealId());
                }
                clearPlanListNew.add(clearplan);
            }
            //正式表数据
            List entityOlds = getListByClearPlanId(clearplanid);
            List<String> bizOpr = new ArrayList<>();//状态
            List<String> realIds = new ArrayList<>();//id
            String approvalRefuseReason = clearPlanList.get(0).getAuditRefuseReason();
            auditStatus = clearPlanList.get(0).getAuditStatus();
            if (Common.isBlank(checkStatus)) {
                log.error("请求参数(opt)异常!");
                throw new RedpException("请求参数(opt)异常!");
            }
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(auditStatus)) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                    //修改正式表数据为新增审批通过，数据状态为正式
                    for (CClearPlan clearplan : clearPlanListNew) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        realIds.add(clearplan.getId());
                        bizOpr.add(BizOprEnum.OPR_ADD.getType());
                        //修改临时表审核状态
                        bizTmpService.saveBizTmp(clearplan, user, user.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                                , String.valueOf(oprMenuId), chkMenuName, BizDataStatusEnum.STATUS_NORMAL.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType(), String.valueOf(clearplan.getId()), webNextUrl, null, null, null);
                    }
                    this.saveOrUpdateBatch(clearPlanListNew);
                    List entitys = clearPlanListNew;
                    //将数据保存到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);
                } else {
                    //修改正式表审核状态为新增审批拒绝
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType();
                    oprstatus = BizDataStatusEnum.STATUS_DRAFT.getType();
                    this.upstatus(clearplanid, auditStatus, !Common.isBlank(oprstatus) ? oprstatus : clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);
                    //修改临时表审核状态为新增审批拒绝，数据状态为非正式
                    for (BizTmp biztmp : bizTmpByMasterKey) {
                        biztmp.setAuditStatus(auditStatus);
                        biztmp.setBizStatus(oprstatus);
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpByMasterKey);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(auditStatus)) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                    //修改正式表审核状态为注销审核通过，数据状态为已注销
                    for (CClearPlan clearplan : clearPlanListNew) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
                        realIds.add(clearplan.getId());
                        bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                        //修改临时表审核状态
                        bizTmpService.saveBizTmp(clearplan, user, user.getInstId(), BizOprEnum.OPR_CANCEL.getType(), lastUpddt
                                , String.valueOf(oprMenuId), chkMenuName, BizDataStatusEnum.STATUS_CANCELED.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType(), String.valueOf(clearplan.getId()), webNextUrl, null, null, null);
                    }
                    this.saveOrUpdateBatch(clearPlanListNew);
                    List entitys = clearPlanListNew;
                    //数据保存到历史表
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);
                } else {
                    //修改正式表审核状态为注销审批拒绝
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType();
                    this.upstatus(clearplanid, auditStatus, clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);
                    //修改临时表审核状态为注销审批拒绝,数据状态为非正式
                    for (BizTmp biztmp : bizTmpByMasterKey) {
                        biztmp.setAuditStatus(auditStatus);
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpByMasterKey);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(auditStatus)) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                    //修改正式表审核状态为停用审批通过
                    for (CClearPlan clearplan : clearPlanListNew) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
                        realIds.add(clearplan.getId());
                        bizOpr.add(BizOprEnum.OPR_STOP.getType());
                        //修改临时表审核状态
                        bizTmpService.saveBizTmp(clearplan, user, user.getInstId(), BizOprEnum.OPR_STOP.getType(), lastUpddt
                                , String.valueOf(oprMenuId), chkMenuName, BizDataStatusEnum.STATUS_STOPED.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType(), String.valueOf(clearplan.getId()), webNextUrl, null, null, null);
                    }
                    this.saveOrUpdateBatch(clearPlanListNew);
                    List entitys = clearPlanListNew;//修改之后
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);
                } else {
                    //修改正式表数据审核状态为停用审批拒绝
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType();
                    this.upstatus(clearplanid, auditStatus, !Common.isBlank(oprstatus) ? oprstatus : clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);
                    //修改临时表数据审核状态为停用审核拒绝,数据状态为非正式
                    for (BizTmp biztmp : bizTmpByMasterKey) {
                        biztmp.setAuditStatus(auditStatus);
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpByMasterKey);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(auditStatus)) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                    //修改正式表数据审核状态为启用审批通过,
                    for (CClearPlan clearplan : clearPlanListNew) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        realIds.add(clearplan.getId());
                        bizOpr.add(BizOprEnum.OPR_USE.getType());
                        //修改临时表审核状态
                        bizTmpService.saveBizTmp(clearplan, user, user.getInstId(), BizOprEnum.OPR_USE.getType(), lastUpddt
                                , String.valueOf(oprMenuId), chkMenuName, BizDataStatusEnum.STATUS_NORMAL.getType(), BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType(), String.valueOf(clearplan.getId()), webNextUrl, null, null, null);
                    }
                    this.saveOrUpdateBatch(clearPlanListNew);
                    List entitys = clearPlanListNew;//修改之后
                    bizHisService.saveBizHisList(entitys, entityOlds, user, user.getInstId(), bizOpr, lastUpddt, String.valueOf(oprMenuId), chkMenuName, realIds, null, null, null);
                } else {
                    //修改正式表数据为启用审批拒绝
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType();
                    this.upstatus(clearplanid, auditStatus, !Common.isBlank(oprstatus) ? oprstatus : clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);
                    //修改临时表数据审核状态为启用审核拒绝,数据状态为非正式
                    for (BizTmp biztmp : bizTmpByMasterKey) {
                        biztmp.setAuditStatus(auditStatus);
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpByMasterKey);
                }
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(auditStatus)) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                    //将临时表数据保存到正式表，审核状态为修改审批通过
                    for (CClearPlan clearplan : clearPlanListNew) {
                        clearplan.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                        clearplan.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
                        realIds.add(clearplan.getId());
                        bizOpr.add(BizOprEnum.OPR_UPDATE.getType());
                        //修改临时表审核状态
                        bizTmpService.saveBizTmp(clearplan, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                                , String.valueOf(oprMenuId), chkMenuName, oprstatus, BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType(), String.valueOf(clearplan.getId()), webNextUrl, null, null, null);
                    }
                    this.mergeData(clearPlanListNew);
                    //将数据保存到历史表
                    this.hisData(clearPlanListNew, entityOlds, String.valueOf(chkMenuId), chkMenuName, user, user.getInstId(), lastUpddt);
                } else {
                    //修改正式表审批状态为修改审批拒绝
                    auditStatus = BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType();
                    this.upstatus(clearplanid, auditStatus, !Common.isBlank(oprstatus) ? oprstatus : clearPlanList.get(0).getOprStatus(), user.getId(), lastUpddt);
                    //修改临时表数据审核状态为修改审核拒绝,数据状态为非正式
                    for (BizTmp biztmp : bizTmpByMasterKey) {
                        biztmp.setAuditStatus(auditStatus);
                        biztmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
                    }
                    bizTmpService.saveOrUpdateBatch(bizTmpByMasterKey);
                }
            }
            //更新第一个tab页数据进行审核
            saveCheck(clearPlanMapId,cid, checkStatus, approvalRefuseReason);
        }

    }


    /**
     * 修改正式表审核状态
     *
     * @param clearplanid
     * @param auditStatus
     * @param impStatus
     * @param lastOprId
     * @param lastUpdateTime
     */
    public void upstatus(String clearplanid, String auditStatus, String impStatus, String lastOprId, String lastUpdateTime) {
        LambdaUpdateWrapper<CClearPlan> wrapper = new LambdaUpdateWrapper<>();

        wrapper.eq(CClearPlan::getClearPlanId, clearplanid).set(CClearPlan::getAuditStatus, auditStatus)
                .set(CClearPlan::getOprStatus, impStatus).set(CClearPlan::getAuditOprId, lastOprId)
                .set(CClearPlan::getUpdateTime, lastUpdateTime);

        this.update(wrapper);

    }

    //停用 启用 注销 需要审核时更新比较临时表数据;因为停用 启用 注销之前可能修改审批不通过 此时正式表和临时表数据对应不起来了
    public void updateTmp(List<CClearPlan> clearPlanList, String bizOpr, String auditStatus, String menuId, String chkMenuName,
                          String webNextUrl, String clearPlanMapId, String instId, UpmpUserInfoVO user, String org) throws RedpException {
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        List<BizTmp> bizTmpList = new ArrayList<>();
        for (CClearPlan clearplan : clearPlanList) {
            BizTmp tmp = new BizTmp();
            String bizData = JSON.toJSONString(clearplan);
            if (bizData.length() > 12000) {
                throw new RedpException("业务数据过长，请联系管理员！");
            }
            String ename = clearplan.getClass().getAnnotation(TableName.class).value().toLowerCase();
            String cname = clearplan.getClass().getAnnotation(Description.class).value();
            String bizData1;
            String bizData2 = "";
            String bizData3 = "";
            if (bizData.length() > 4000) {
                bizData1 = bizData.substring(0, 3999);
                bizData2 = bizData.substring(4000);
            } else {
                bizData1 = bizData;
            }
            if (bizData2.length() > 4000) {
                bizData2 = bizData2.substring(0, 3999);
                bizData3 = bizData.substring(8000);
            }

            tmp.setOprId(user.getId());
            tmp.setBizData1(bizData1);
            tmp.setBizData2(bizData2);
            tmp.setBizData3(bizData3);
            tmp.setBizOpr(bizOpr);
            tmp.setCreateTime(lastUpddt);
            tmp.setLastOprId(user.getId());
            tmp.setLastUpdateTime(lastUpddt);
            tmp.setOprMenuId(menuId);
            tmp.setOprMenuName(chkMenuName);
            tmp.setOprTableCname(cname);
            tmp.setOprTableEname(ename);
            tmp.setManageInstId(org);
            tmp.setInstId(instId);
            tmp.setBizStatus(BizDataStatusEnum.STATUS_DRAFT.getType());
            tmp.setAuditStatus(auditStatus);
            tmp.setDetailFlag("Y");
            tmp.setMasterKey(clearPlanMapId);
            tmp.setRealId(clearplan.getId());
            tmp.setWebNextUrl(webNextUrl);
            bizTmpList.add(tmp);
        }
        this.tmpData(bizTmpList, clearPlanMapId, instId);
    }

    //从第二个tab页调用审批时，此时保存审核状态数据
    public void saveCheck(String clearPlanMapId,String cid ,String checkStatus, String approvalRefuseReason) throws RedpException {
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);

        //获取临时表数据并转换成实体对象
        BizTmp tmp = bizTmpService.getBizTmpDetail(clearPlanMapId);
        String bizdata = "";
        if (null != tmp.getBizData1()) {
            bizdata += tmp.getBizData1();
        }
        if (null != tmp.getBizData2()) {
            bizdata += tmp.getBizData2();
        }
        if (null != tmp.getBizData3()) {
            bizdata += tmp.getBizData3();
        }
        //临时表数据
        CClearPlanMap clearplanmapNew = JSON.parseObject(bizdata, CClearPlanMap.class);
        //正式表数据
        CClearPlanMap clearplanmap = clearPlanMapService.findByClearPlanId(cid);
        //备份正式表数据
        CClearPlanMap clearplanmapOld = new CClearPlanMap();
        BeanUtils.copyProperties(clearplanmap, clearplanmapOld);

        if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
            // 审批通过将临时表数据更新到正式表
            BeanUtils.copyProperties(clearplanmapNew, clearplanmap);
        } else {
            clearplanmap.setAuditRefuseReason(approvalRefuseReason);
        }
        clearplanmap.setAuditTime(lastUpddt);
        clearplanmap.setAuditOprId(user.getId());

        String bizOpr = "";
        if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(clearplanmap.getAuditStatus())) {
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                clearplanmap.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
            } else {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
            }
            bizOpr = BizOprEnum.OPR_ADD.getType();
        } else if (BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_CHECKING.getType().equals(clearplanmap.getAuditStatus())) {
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_PASS.getType());
                clearplanmap.setOprStatus(BizDataStatusEnum.STATUS_CANCELED.getType());
            } else {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_CANCEL_REJECT.getType());
            }
            bizOpr = BizOprEnum.OPR_CANCEL.getType();
        } else if (BizDataAuditStatusEnum.AUDIT_STATUS_STOP_CHECKING.getType().equals(clearplanmap.getAuditStatus())) {
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_PASS.getType());
                clearplanmap.setOprStatus(BizDataStatusEnum.STATUS_STOPED.getType());
            } else {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_STOP_REJECT.getType());
            }
            bizOpr = BizOprEnum.OPR_STOP.getType();
        } else if (BizDataAuditStatusEnum.AUDIT_STATUS_USE_CHECKING.getType().equals(clearplanmap.getAuditStatus())) {
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_PASS.getType());
                clearplanmap.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
            } else {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_USE_REJECT.getType());
            }
            bizOpr = BizOprEnum.OPR_USE.getType();
        } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(clearplanmap.getAuditStatus())) {
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                clearplanmap.setOprStatus(BizDataStatusEnum.STATUS_NORMAL.getType());
            } else {
                clearplanmap.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
            }
            bizOpr = BizOprEnum.OPR_UPDATE.getType();
        }
        clearPlanMapService.saveOrUpdate(clearplanmap);

        if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(checkStatus)) {
            //审批通过将正式表更新到历史表
            bizHisService.saveBizHisNew(clearplanmap, clearplanmapOld, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt,
                    String.valueOf(oprMenuId), oprMenuName, clearplanmap.getId(), null, null, null);

        }
        // 修改临时表的审核状态等字段
        bizTmpService.saveBizTmp(clearplanmap, user, user.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt,
                String.valueOf(oprMenuId), oprMenuName, clearplanmap.getOprStatus(), clearplanmap.getAuditStatus(),
                clearplanmap.getId(), webNextUrl, null, null, null);

    }


    /**
     * 新增、修改、删除明细记录
     *
     * @param clearPlanList
     * @throws RedpException
     */
    public void mergeData(List<CClearPlan> clearPlanList) throws RedpException {

        List<CClearPlan> listOrg = getListByClearPlanId(clearPlanList.get(0).getClearPlanId());
        List<CClearPlan> listDel = new ArrayList<CClearPlan>();//待删除的数据
        List<CClearPlan> listNew = new ArrayList<CClearPlan>();//待新增的数据

        if (null != listOrg) {//分析出待删除的数据
            Iterator<CClearPlan> itMsir = listOrg.iterator();
            while (itMsir.hasNext()) {
                CClearPlan msto = (CClearPlan) itMsir.next();
                int flag = 0;

                Iterator<CClearPlan> itMsi = clearPlanList.iterator();
                while (itMsi.hasNext()) {
                    CClearPlan mst = (CClearPlan) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { //临时表中的数据在此次提交时已经被删除
                    listDel.add(msto);
                }
            }
        }

        if (null != clearPlanList) {//分析出待新增的数据
            Iterator<CClearPlan> itMsir = clearPlanList.iterator();
            while (itMsir.hasNext()) {
                CClearPlan mst = (CClearPlan) itMsir.next();
                int flag = 0;

                Iterator<CClearPlan> itMso = listOrg.iterator();
                while (itMso.hasNext()) {
                    CClearPlan msto = (CClearPlan) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    listNew.add(mst);
                }
            }
        }
        if (Common.isNotBlank(listOrg) && listOrg.size() > 0) {//修改
            for (CClearPlan clearPlan : listOrg) {
                LambdaUpdateWrapper<CClearPlan> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(CClearPlan::getClearPlanId, clearPlan.getClearPlanId()).eq(CClearPlan::getClearSeq, clearPlan.getClearSeq());
                baseMapper.update(clearPlan, wrapper);
            }
//            this.saveOrUpdateBatch(listOrg);
        }
        if (Common.isNotBlank(listNew) && listNew.size() > 0) {//新增
            for (CClearPlan clearplan : listNew) {
                if (org.apache.commons.lang3.StringUtils.isEmpty(clearplan.getId())) {
                    clearplan.setId(null);
                }
                String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
                clearplan.setCreateTime(lastUpddt);
            }

            for (CClearPlan clearPlan : listNew) {
                baseMapper.insert(clearPlan);
            }
//            this.saveOrUpdateBatch(listNew);
        }
        if (Common.isNotBlank(listDel) && listDel.size() > 0) {//删除
            for (CClearPlan clearPlan : listOrg) {
                LambdaUpdateWrapper<CClearPlan> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(CClearPlan::getClearPlanId, clearPlan.getClearPlanId()).eq(CClearPlan::getClearSeq, clearPlan.getClearSeq());
                baseMapper.delete(wrapper);
            }
        }
    }

    /**
     * 查与clearPlanId值相等的数据
     *
     * @param clearPlanId
     * @return
     */
    private List<CClearPlan> getListByClearPlanId(String clearPlanId) {
        LambdaQueryWrapper<CClearPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CClearPlan::getClearPlanId, clearPlanId);
        return baseMapper.selectList(wrapper);
    }


    /**
     * 新增、修改、删除临时表明细记录
     *
     * @param bizTmpList
     * @param clearPlanMapId
     * @param instId
     * @throws RedpException
     */
    public void tmpData(List<BizTmp> bizTmpList, String clearPlanMapId, String instId) throws RedpException {

        List<BizTmp> listOrg = bizTmpService.getBizTmpByMasterKey(instId, clearPlanMapId);
        List<BizTmp> listDel = new ArrayList<BizTmp>();//待删除的数据
        List<BizTmp> listNew = new ArrayList<BizTmp>();//待新增的数据


        if (null != listOrg) {//分析出待删除的数据
            Iterator<BizTmp> itMsir = listOrg.iterator();
            while (itMsir.hasNext()) {
                BizTmp msto = (BizTmp) itMsir.next();
                int flag = 0;

                Iterator<BizTmp> itMsi = bizTmpList.iterator();
                while (itMsi.hasNext()) {
                    BizTmp mst = (BizTmp) itMsi.next();
                    if (msto.getRealId().equals(mst.getRealId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) { //临时表中的数据在此次提交时已经被删除
                    listDel.add(msto);
                }
            }
        }

        if (null != bizTmpList) {//分析出待新增的数据
            Iterator<BizTmp> itMsir = bizTmpList.iterator();
            while (itMsir.hasNext()) {
                BizTmp mst = (BizTmp) itMsir.next();
                int flag = 0;

                Iterator<BizTmp> itMso = listOrg.iterator();
                while (itMso.hasNext()) {
                    BizTmp msto = (BizTmp) itMso.next();
                    if (msto.getRealId().equals(mst.getRealId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) { // 临时表中不存在的数据
                    listNew.add(mst);
                }
            }
        }
        if (Common.isNotBlank(listOrg) && listOrg.size() > 0) {//修改
            bizTmpService.saveOrUpdateBatch(listOrg);
        }
        if (Common.isNotBlank(listNew) && listNew.size() > 0) {//新增
            bizTmpService.saveOrUpdateBatch(listNew);
        }
        if (Common.isNotBlank(listDel) && listDel.size() > 0) {//删除
            bizTmpService.delete(listDel);
        }
    }

    /**
     * 历史表记录
     *
     * @param dbclearplan
     * @param clearplanNew
     * @param menuId
     * @param chkMenuName
     * @param user
     * @param org
     * @param lastUpddt
     */
    private void hisData(List<CClearPlan> dbclearplan, List<CClearPlan> clearplanNew, String menuId, String chkMenuName, UpmpUserInfoVO user, String org, String lastUpddt) throws RedpException {

        List entitys = new ArrayList();//新纪录
        List entityOlds = new ArrayList();//原纪录
        List<String> bizOpr = new ArrayList<>();
        List<String> realIds = new ArrayList<>();

        if (null != clearplanNew) {//删除的数据
            Iterator<CClearPlan> itMsir = clearplanNew.iterator();
            while (itMsir.hasNext()) {
                CClearPlan msto = (CClearPlan) itMsir.next();
                int flag = 0;

                Iterator<CClearPlan> itMsi = dbclearplan.iterator();
                while (itMsi.hasNext()) {
                    CClearPlan mst = (CClearPlan) itMsi.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) {
                    entitys.add(new CClearPlan());
                    entityOlds.add(msto);
                    bizOpr.add(BizOprEnum.OPR_CANCEL.getType());
                    realIds.add(msto.getId());
                }
            }
        }

        if (null != dbclearplan) {//新增的数据
            Iterator<CClearPlan> itMsir = dbclearplan.iterator();
            while (itMsir.hasNext()) {
                CClearPlan mst = (CClearPlan) itMsir.next();
                int flag = 0;

                Iterator<CClearPlan> itMso = clearplanNew.iterator();
                while (itMso.hasNext()) {
                    CClearPlan msto = (CClearPlan) itMso.next();
                    if (msto.getId().equals(mst.getId())) {
                        flag = 1;
                        BeanUtils.copyProperties(mst, msto);
                        break;
                    }
                }
                if (flag == 0) {
                    entitys.add(mst);
                    entityOlds.add(new CClearPlan());
                    bizOpr.add(BizOprEnum.OPR_ADD.getType());
                    realIds.add(mst.getId());
                }
            }
        }


        for (int j = 0; j < clearplanNew.size(); j++) {
            for (int i = 0; i < dbclearplan.size(); i++) {
                if (dbclearplan.get(i).getId().equals(clearplanNew.get(j).getId())) {
                    entitys.add(dbclearplan.get(i));
                    entityOlds.add(clearplanNew.get(j));
                    bizOpr.add(BizOprEnum.OPR_UPDATE.getType());
                    realIds.add(clearplanNew.get(j).getId());
                }
            }
        }

        bizHisService.saveBizHisList(entitys, entityOlds, user, org, bizOpr, lastUpddt, menuId, chkMenuName, realIds, null, null, null);

    }


}
